clear
close all
clear persistent
clear global
drawnow;
CharAvail;% call it now to load it
KbCheck;
when=0; % how many screen refreshes to wait before flip
dontclear=0;% 0=copy buffer forward and clear buffer; 1=copy buffer forward and leave the buffer as is. 2=flip the two (fastest)
dontsync=1; % 0=wait and sync; 1=don't wait but sinc; 2=neither wait nor sync
multiflip=0;% for multi screens, bad idea
screenid=1; % for multi screens, always select the primary monitor
getExtendedData=0;
getRawCode=1;
ListenChar(0)
%Screen('Preference', 'SkipSyncTests', 1);
Screen('Preference', 'VisualDebuglevel', 3); % 4 is default but it makes a painful white screen
%Screen('Preference', 'ConserveVRAM', 4096);
%Screen('Preference', 'ConserveVRAM',65536);
flicker_dur=((1000/60)*10)/1000;
block_dur=4;
toggle=0;
toggle2=0;
firsttime=1;
flicker_time=flicker_dur:flicker_dur:block_dur;
block_time=block_dur:block_dur:block_dur*4+block_dur;
b_i=1;
f_i=1;
EPI_TTL_Pulse=0;
VBLTimestamp=[]; % doesn't always work
StimulusOnsetTime=[]; % doesn't always work
FlipTimestamp=[]; % flip time, when the display is actually presented
Missed=[];
Beampos=[];
ch=' ';% what key was pressed
i=0;
j=0;
screenit=0;

thing1=[];
[w, screenRect] = Screen('OpenWindow', screenid);%[0 0 0]
%[w, screenRect]=Screen('OpenWindow', 0, 0);
fprintf('\nstart test\n')
screen('fillrect',w,[ 0  0  0]*255);
framerate=Screen('NominalFramerate', w);
center_x=screenRect(3)/2;
center_y=screenRect(4)/2;
%screen('DrawText',w,'waiting for magnet to start',center_x,center_y,[ 0 1 0]*255);%-screen('textwidth',w,kk{min(find(keyCode))})/2
DrawFormattedText(w,'waiting for magnet to start','center','center',[ 0 1 0]*255)
screen('flip',w);
screen('fillrect',w,[ .5 .5 .5]*255);
%fprintf('\n%5d,%5d,(%c)\n',i,j,ch(end))
fprintf('waiting for magnet to start...\n')
ListenChar(0)
pause;
ListenChar(2);% shuts off keyboard output to windows
screen('flip',w);
secs=getsecs;
fliptime=secs+flicker_dur;blocktime=secs+block_dur*2;
flickertime=flicker_time+block_time(1);
block_time=block_time+secs;
fprintf('started...\n')
while b_i<=length(block_time)
	if block_time(b_i)<=getsecs
		b_i=b_i+1;
		f_i=1;
		%block_time(b_i)
		flickertime=flicker_time+block_time(b_i-1);
		%flickertime-getsecs
		firsttime=1;
	end
	if mod(b_i,2)==1
		if firsttime
			screen('fillrect',w,[ .1 .1 .1]*255);screenit=1;
			fprintf('baseline\n')
			firsttime=0;
		end
		%fprintf('baseline\n')
	else % mod(b_i,2);
		if firsttime
			fprintf('flicker\n')
			firsttime=0;
		end
		
		if flickertime(f_i)<=getsecs
			%(flickertime(f_i)-secs)*1000
			f_i=f_i+1;
			thing1(end+1)=floor((flickertime(f_i)-secs)*1000);
			toggle=abs(toggle-1);
			%mod(f_i,2)
			%if mod(f_i,2)==1;
			if toggle
				screen('fillrect',w,[ 0  1  1]*255);screenit=1;
				%fprintf('odd\n')
			else
				screen('fillrect',w,[ 0  0  0]*255);screenit=1;
				%fprintf('even\n')
			end
		end % if fliptime
	end % toggle2
	if screenit
		[VBLTimestamp(end+1) StimulusOnsetTime(end+1) FlipTimestamp(end+1) Missed(end+1) Beampos(end+1)] = Screen('Flip', w, when, dontclear, dontsync, multiflip);
		screenit=0;
	end
	%
	% 		[VBLTimestamp(end+1) StimulusOnsetTime(end+1) FlipTimestamp(end+1) Missed(end+1) Beampos(end+1)] = Screen('Flip', w, when, dontclear, dontsync, multiflip);
	%
	% 		if CharAvail
	% 			secs(end+1)=getsecs;
	% 			ch(end+1) = GetChar(getExtendedData, getRawCode);
	% 			j=j+1; % # of charaters taken from buffer
	% 			switch ch(end) %  ascii numbers which are easier to deal with
	% 				case 48+1 % button 1
	% 					screen('fillrect',w,[ 0  0  1]*255);
	% 				case 48+2 % button 2
	% 					screen('fillrect',w,[ 1  1  0]*255);
	% 				case 48+3 % button 3
	% 					screen('fillrect',w,[ 0  1  0]*255);
	% 				case 48+4 % button 4
	% 					screen('fillrect',w,[ 1  0  0]*255);
	% 				case 48+5 % EPI TTL Pulse
	% 					screen('fillrect',w,[ 1  1  1]*255);
	% 					EPI_TTL_Pulse=EPI_TTL_Pulse+1;
	% 				case 48+6 % button 6
	% 					screen('fillrect',w,[ 0  1  1]*255);
	% 				case 48+7 % button 7
	% 					screen('fillrect',w,[ 1 .5  0]*255);
	% 				case 48+8 % button 8
	% 					screen('fillrect',w,[ 0  0  0]*255);
	% 				case 48+9 % button 9
	% 					screen('fillrect',w,[ 1  0  1]*255);
	% 				case 27 % ESC on pcwin
	% 					break
	% 				otherwise
	% 					screen('fillrect',w,[ 0  0  0]*255);
	% 					if ch(end)>0 & ch(end)<128
	% 						screen('DrawText',w,kk{ch(end)},center_x,center_y,[ 0  1  0]*255);
	% 					else
	% 						screen('DrawText',w,num2str(ch(end)),center_x,center_y,[ 1  0  0]*255);
	% 					end
	% 			end
	% 			[VBLTimestamp(end+1) StimulusOnsetTime(end+1) FlipTimestamp(end+1) Missed(end+1) Beampos(end+1)] = Screen('Flip', w, when, dontclear, dontsync, multiflip);
	% 		else% if ~CharAvail
	[keyIsDown,secsa,keyCode] = KbCheck;
	if keyCode(27) % when CharAvail fails this is the only way to get out of the infinite Loop
		fprintf('esc\n')
		%secs(end+1)=secsa;
		break
	end
	% 			%waitsecs(.01); % a suggestion to help slow the program down (useless in real experiment)
	% 			i=i+1;% # sdfdsfgsdfgsdfgof loops where nothing was taken from buffer
	% 		end% if CharAvail
end% while
fprintf('\nend test\n')
ListenChar(0)
SCREEN('closeall');
diff(floor(VBLTimestamp*1000))-(flicker_dur*1000)
diff(floor(StimulusOnsetTime*1000))-(flicker_dur*1000)
diff(thing1)-(flicker_dur*1000)
